package Demo2;

import java.util.Arrays;
import java.util.Comparator;

public class Test2 {
    //    public static void main(String[] args) {
//        //数组常用API
//        //binarySearch - 使用binarySearch方法时需要原数组有序 - 底层使用的是二分查找
////        int[] array = {2, 4, 6, 8, 9, 10, 1, 0, 3, 5};
////        Arrays.sort(array);
////        System.out.println(Arrays.toString(array));
////        int i = Arrays.binarySearch(array, 3);
////        System.out.println(i);
//
//
//        //使用比较器逆序排列数组
//        //由于涉及到了泛型，所以必须是Integer类型的数组
////        Integer[] array = {2, 4, 6, 8, 9, 10, 1, 0, 3, 5};
////        Arrays.sort(array,new Comparator<Integer>(){
////            @Override
////            public int compare(Integer o1, Integer o2) {
////                return o2 - o1;
////            }
////        });
////        System.out.println(Arrays.toString(array));
//
////        Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
////        Arrays.sort(array, new Comparator<Integer>() {
////            @Override
////            public int compare(Integer o1, Integer o2) {
////                return o2 - o1;
////            }
////        });
////        for (int i = 0; i < array.length; i++) {
////            System.out.print(array[i]+" ");
////        }
//
//
//
//    }
    public static void main(String[] args) {
        //使用比较器来对二维数组进行排序
//        int[][] array = {{1, 9}, {2, 5}, {19, 20}, {10, 11}, {12, 20}, {0, 3}, {0, 1}, {0, 2}};
//        Arrays.sort(array,new Comparator<int[]>(){
//            public int compare(int[] o1,int[] o2){
//                return o1[1] - o2[1];
//            }
//        });
//        System.out.println(Arrays.deepToString(array));


        //数组复制方法
        /*
        copyOf方法
        * */

//        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//        int[] newArray = new int[array.length];
////        int[] ret = Arrays.copyOf(array, 20);
////        int[] ret = Arrays.copyOfRange(array,3,5);
//        System.arraycopy(array,0,newArray,0,5);
//        for (int i = 0; i < newArray.length; i++) {
//            System.out.print(newArray[i]+ " ");
//        }

        //复制数组的方式
        /*
        copyOf
        copyOfRange
        ------------------
        System.arraycopy - 不会产生新的数组
        * */

//        int[] array = {1, 2, 3, 4, 5};
//        //使用copyOf进行数组扩容
//        int[] newArray = Arrays.copyOf(array,10);
//        System.out.println(Arrays.toString(newArray));


//        int[] originalArray = {1, 2, 3, 4, 5};
////        int indexToRemove = 2; // 要删除的元素的索引位置
////
////        // 创建新数组存储被删除元素之前的部分
////        int[] beforeArray = Arrays.copyOfRange(originalArray, 0, indexToRemove);
////
////        // 创建新数组存储被删除元素之后的部分
////        int[] afterArray = Arrays.copyOfRange(originalArray, indexToRemove + 1, originalArray.length);
////
////        // 将两个新数组合并成一个新数组
////        int[] newArray = new int[beforeArray.length + afterArray.length];
////        System.arraycopy(beforeArray, 0, newArray, 0, beforeArray.length);
////        System.arraycopy(afterArray, 0, newArray, beforeArray.length, afterArray.length);
////
////        // 输出新数组
////        System.out.println("新数组: " + Arrays.toString(newArray));
//
//
//        int index = 2;
//        for(int i = index;i<originalArray.length-1;i++){
//            originalArray[i] = originalArray[i+1];
//        }
//        int[] newArray = Arrays.copyOf(originalArray, originalArray.length - 1);
//        System.out.println(Arrays.toString(newArray));

        //数组内容填充 - fill
//        int[] array = {1, 2, 3, 4, 5};
//        Arrays.fill(array,0);
//        for (int i = 0; i < array.length; i++) {
//            System.out.print(array[i]+ " ");
//        }

        //equals
//        int[] array1 = {1, 2, 3,4};
//        int[] array2 = {1, 2, 3};
//        System.out.println(Arrays.equals(array1,array2));


        int index = 2;
        int[] originalArray = {1, 2, 3, 4, 5};
        int[] before = Arrays.copyOfRange(originalArray, 0, index);
        int[] after = Arrays.copyOfRange(originalArray, index + 1, originalArray.length);
        int[] array = new int[before.length+after.length];
        System.arraycopy(before,0,array,0,before.length);
        System.arraycopy(after, 0, array, before.length , after.length);
        System.out.println(Arrays.toString(array));

    }

}
